package registration;


import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import reservation.DB_Reservation;;

public class Registration extends HttpServlet {
        private static final long serialVersionUID = 1L;        
       
    
    public Registration() {
        super();
        
    }

        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                
        }
                
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                                
                        //pobranie danych z formularza
        						request.setCharacterEncoding("UTF-8");
                                String login = request.getParameter("login");
                                String haslo = request.getParameter("haslo");
                                String imie = request.getParameter("imie");
                                String nazwisko = request.getParameter("nazwisko");
                                String telefonString = request.getParameter("telefon");
                                Integer telefon = Integer.valueOf(telefonString);
                                String mail = request.getParameter("mail");
                                String ulica = request.getParameter("ulica");
                                String nr_domuString = request.getParameter("nr_domu");
                                Integer nr_domu = Integer.valueOf(nr_domuString);
                                String nr_lokalu = request.getParameter("nr_lokalu");
                                                                
                                if (nr_lokalu == ""){
                                        nr_lokalu= "0";
                                }else{
                                        nr_lokalu = request.getParameter("nr_lokalu");
                                }
                                //Integer nr_lokalu = Integer.valueOf(nr_lokaluString);
                                String kod = request.getParameter("kod");
                                String miejscowosc = request.getParameter("miejscowosc");       
                                
                                System.out.println(imie + " " + nazwisko +" "+ ulica);
                                //utworzenie sesji dla wybranych zmiennych (beda potrzebne w confirm.jsp)
                                HttpSession session = request.getSession();
                                session.setAttribute("Imie", imie);
                                session.setAttribute("Nazwisko", nazwisko);
                                session.setAttribute("Mail", mail);
                                
                                DataBase db = new DataBase();                                   
                                
                                try {   
                                	//sprawdzenie czy podany login i adres mail znajduje sie w bazie danych
                                		if(db.checkLoginAndMail(login, mail, request) == true){
                                			forward("jsp/exist.jsp", request, response);
                                		}
                                        else{
                                                //wpisanie uzytkownika do Bazy
                                                db.registerUser(login, haslo, imie, nazwisko, telefon, mail, ulica, nr_domu, nr_lokalu, kod, miejscowosc);
                                                
                                                
                                                //pobranie danych z sesji                                               
                                                java.sql.Date data1 = (Date) session.getAttribute("dejta1");
                                                java.sql.Date data2 = (java.sql.Date)session.getAttribute("dejta2");
                                                String price = (String)session.getAttribute("prajse");
                                                int cena = Integer.parseInt(price);
                                                String id_room = (String)session.getAttribute("id");
                                                int id_rom = Integer.parseInt(id_room);
                                                int id_clients = 0;
                                                id_clients = db.getId_client(login);                                            
                                                                                                
                                                DB_Reservation res = new DB_Reservation();
                                                //zapisanie Rezerwacji do bazy
                                                res.addToDB(id_clients, id_rom, cena, data1, data2);
                                                System.out.println("Zapisano do DB");
                                                //zmiana pokoju na 'zajety'
                                                res.updateRoom(id_rom);
                                                System.out.println("Wykonano Update'a!!!");
                                                forward("jsp/confirm.jsp", request, response);

                                        }
                                } catch (ClassNotFoundException e) {
                                        e.printStackTrace();
                                } catch (SQLException e) {
                                        e.printStackTrace();
                                }               
                                                                
        }
        
        //metoda przekierwujaca do podanej strony jsp
        protected void forward(String jspFileName, HttpServletRequest request, HttpServletResponse response) 
                        throws ServletException, IOException{
                
                request.getRequestDispatcher(jspFileName).forward(request, response);
        }
        

}